interface BaseFormConfig {
    labelAlign: 'left' | 'right',
    layout: 'horizontal' | 'vertical' | 'inline'
    labelCol: Object,
    labelWrap?: true | false
}
interface FormConfig extends BaseFormConfig {
    options: any[]
}

interface componentConfig {
    [key: string]: {
        component: string,
        label?: string,
        attrs: Object,
        slots?: any[],
        rules?: any[]
    }
}
function createComponent(component: string, label: string, attrs: Object, slots: any[], rules: any[]) {
    return {
        component,
        label,
        attrs,
        slots,
        rules
    }
}

class createForm {
    FormRules: Object
    FormConfig: FormConfig
    constructor(ComponentConfig: componentConfig, BaseFormConfig?: BaseFormConfig) {
        this.FormRules = this.createFormRules(ComponentConfig)
        this.FormConfig = this.createForm(BaseFormConfig ?? { labelAlign: 'left', labelCol: { span: 4 }, layout: 'vertical' }, ComponentConfig)

    }
    createForm({ labelAlign, layout, labelCol }: BaseFormConfig, ComponentConfig: any,) {
        // console.log(ComponentConfig);

        let options: any[] = []
        for (const key in ComponentConfig) {
            options.push({
                name: key,
                ...ComponentConfig[key as keyof typeof ComponentConfig]
            })
        }
        return {
            labelAlign,
            layout, //'horizontal'|'vertical'|'inline'
            labelCol,
            options
        }
    }
    // 验证规则
    createFormRules(FieldName: any) {
        let rules: any = {}
        for (const key in FieldName) {
            if (FieldName[key as keyof typeof FieldName].rules) {
                rules[key] = FieldName[key as keyof typeof FieldName].rules
            }
        }
        return rules
    }
}


export {
    createComponent,
    createForm
}